﻿
namespace DAPNet
{
    internal class ClickWidener : IClickDeterminator
    {
        internal enum Directions
        {
            Left,
            Right,
            Both
        }


        private int distance;

        private Directions direction;


        internal ClickWidener(int distance, Directions direction)
        {
            this.distance = distance;
            this.direction = direction;
        }

        internal int Distance
        {
            get 
            { 
                return distance; 
            }
            set 
            { 
                distance = value; 
            }
        }

        internal Directions Direction
        {
            get 
            { 
                return direction; 
            }
            set
            { 
                direction = value; 
            }
        }

        public void Determinate(ClickDetection detection)
        {
            switch (direction)
            {
                case Directions.Left:
                    WidenLeft(detection);
                    break;
                case Directions.Right:
                    WidenRight(detection);
                    break;
                case Directions.Both:
                    WidenLeft(detection);
                    WidenRight(detection);
                    break;
                default:
                    break;
            }
        }

        private void WidenRight(ClickDetection detection)
        {
            int numConsecutivePositives = 0;
            for (int detectionIndex = 0; detectionIndex < detection.Count; detectionIndex++)
            {
                if (detection[detectionIndex])
                {
                    numConsecutivePositives = distance;
                }
                else if (numConsecutivePositives > 0)
                {
                    detection[detectionIndex] = true;
                    numConsecutivePositives--;
                }
            }
        }

        private void WidenLeft(ClickDetection detection)
        {
            int numConsecutivePositives = 0;
            for (int detectionIndex = detection.Count - 1; detectionIndex > 0; detectionIndex--)
            {
                if (detection[detectionIndex])
                {
                    numConsecutivePositives = distance;
                }
                else if (numConsecutivePositives > 0)
                {
                    detection[detectionIndex] = true;
                    numConsecutivePositives--;
                }
            }
        }
    }
}
